<?php
namespace Home\Controller;
use Think\Controller;
class LifeController extends Controller {
    public function showlist()
    {
        //获取传递过来的参数
        $cate_id = I('get.cate_id');
        if(!empty($_GET['blog_id'])){
            $blog_id=I('get.blog_id');
            $this->news($cate_id,$blog_id);
        }else{
            //获取相册列表中前4个相册
            $pics_info = D('pics as p')
                ->join('bg_cate as c on p.cate_id=c.cate_id')
                ->order('pics_id desc')
                ->limit(4)
                ->select();
            $this->assign('pics_info', $pics_info);
            //获取blog中总的条数
            $rows = D('Blog')->where(array('cate_id'=>$cate_id))->count();
            //每页的条数
            $per = 2;
            //引入分页类
            $page = new \Think\Page($rows,$per);
            //分页类配置
            $page->setConfig('header','共 %TOTAL_ROW% 条记录');
            $page->setConfig('prev','<<');
            $page->setConfig('next','>>');
            $page->setConfig('first','1...');
            $page->setConfig('last','...%TOTAL_PAGE%');
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
            $pagelist = $page->show();
            //根据参数获取该分类下的博文列表
            $life_info = D('Blog as b')
                ->join('bg_cate as c on b.cate_id=c.cate_id')
                ->where("b.cate_id='{$cate_id}'")
                ->limit($page->firstRow.','.$page->listRows)
                ->select();
            //获取最新8条博文内容
            $news_blog = D('Blog as b')
                ->join('bg_cate as c on b.cate_id=c.cate_id')
                ->where("b.is_show = '0'")
                ->order('blog_id desc')
                ->limit('8')
                ->select();
            $this->assign('news_blog',$news_blog);
            $this->assign('pagelist',$pagelist);
            $this->assign('life_info',$life_info);
            //获取分类信息
            $cate_info = D('Cate')->select();
            $this->assign('cate_info',$cate_info);
            $this->display();
        }
    }
    public function news($cate_id,$blog_id){
        //获取相册列表中前4个相册
        $pics_info = D('pics as p')
            ->join('bg_cate as c on p.cate_id=c.cate_id')
            ->order('pics_id desc')
            ->limit(4)
            ->select();
        $this->assign('pics_info', $pics_info);
        //获取最新8条博文内容
        $news_blog = D('Blog as b')
            ->join('bg_cate as c on b.cate_id=c.cate_id')
            ->where("b.is_show = '0'")
            ->order('blog_id desc')
            ->limit('8')
            ->select();
        $this->assign('news_blog', $news_blog);
        //获取当前小于$blog_id下的文章信息
        $blogs_pre_info = D('Blog')
            ->where("cate_id={$cate_id} and blog_id < ".$blog_id)
            ->find();
        $this->assign('pre_info',$blogs_pre_info);
        //获取当前$blog_id下的文章内容
        $blogs_info = D('Blog')
            ->where(array('blog_id'=>$blog_id))
            ->find();
        $this->assign('info',$blogs_info);
        //获取当前大于$blog_id下的文章信息
        $blogs_next_info = D('Blog')
            ->where("cate_id={$cate_id} and blog_id > ".$blog_id)
            ->find();
        $this->assign('next_info',$blogs_next_info);

        //获取分类信息
        $cate_info = D('Cate')->select();
        $this->assign('cate_info',$cate_info);
        $this->display('news');
    }
}